import { Component, Input, OnInit } from '@angular/core';
import { ActivityProductService } from '@core/routes-services/activity-product.service';
import { MoonCakeService } from '@core/routes-services/moon-cake.service';
import { ActivatedRoute } from '@angular/router';
import { CakeOrderViewComponent } from '../../mid-autumn/cake-order/order-view/cake-order-view.component';
import { MyModalHelper } from '@core/my-modal.helper';
import {
  CAKE_ORDER_VOUCHER_STATUS_OPTION,
  FINANCES_IN_TYPE,
  FINANCES_TYPE, ORDER_STATUS_OPTION,
  USER_ROLE_OPTION,
} from '../../../constant/dictionary';
import { FinancesService } from '@core/routes-services/finances.service';
import { NzMessageService } from 'ng-zorro-antd';
import { OrderViewComponent } from '../../order/order-view/order-view.component';
import { OrderService } from '@core/routes-services/order.service';

@Component({
  selector: 'app-agent-finance',
  templateUrl: './agent-finance.component.html',
})
export class AgentFinanceComponent implements OnInit {
  @Input() agentId:any;
  @Input() financeType: any;
  isLoading = false;
  keyword: any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };
  userRoleOption: any [] = USER_ROLE_OPTION;
  financesType: any [] = FINANCES_TYPE;
  FinancesInType: any [] = FINANCES_IN_TYPE;
  orderStatusOption: any [] = ORDER_STATUS_OPTION;
  allProducts: any [] = [];

  constructor(private message: NzMessageService,
              private route: ActivatedRoute,
              private financesService: FinancesService,
              private activityProductService: ActivityProductService,
              private moonCakeService:MoonCakeService,
              private orderService:OrderService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initProduct();
    this.initPage();
  }

  initProduct() {
    this.activityProductService.getAll().then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.allProducts = res.data.activityProductsList;
      } else {
        this.message.warning(res.message);
      }
    }, error => {
      this.message.error(error.message ? error.message : error.error);
    });
  }

  initPage() {
    this.isLoading = true;
    this.financesService.allPagination(this.page, this.keyword, this.agentId,this.financeType).then((res) => {
      console.log(res);
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  /**
   * 查看详情
   * data.type ==1 || data.type ==4 查看云订单详情
   * data.type == 2 && data.inType == 3 查看星创订单详情
   * @param data
   */
  seeEntity = (data) => {
    if(data.type == 2 && data.inType == 3){
      this.moonCakeService.getByOrderNo(data.remark).then((res) => {
        if (res.status.toString().startsWith('20')) {
          if(!res.data){
            this.message.warning("查无此订单");
            return;
          }
          this.modalHelper.open(CakeOrderViewComponent, {
            entity: res.data,
            allProducts: this.allProducts,
            statusOption: CAKE_ORDER_VOUCHER_STATUS_OPTION,
          }, 'lg', {
            nzTitle: '订单信息',
          }).subscribe(result => {
            console.log('弹窗回调ok');
          }, (error) => {
            console.error('弹窗回调出错：', error);
          });
        } else {
          this.message.warning(res.message);
        }
      }, error => {
        this.message.error(error.message ? error.message : error.error);
      });
    }else if(data.type == 1 || data.type == 4){
      this.orderService.detail(data.relateId).then((res) => {
        if (res.status.toString().startsWith('20')) {
          if(!res.data){
            this.message.warning("查无此订单");
            return;
          }
          this.modalHelper.open(OrderViewComponent, { entity: res.data,orderStatusOption: this.orderStatusOption }, 'lg', {
            nzTitle: '订单信息',
          }).subscribe(result => {
            console.log('弹窗回调ok');
          }, (error) => {
            console.error('弹窗回调出错：', error);
          });
        } else {
          this.message.warning(res.message);
        }
      }, error => {
        this.message.error(error.message ? error.message : error.error);
      });
    }
  };

}
